﻿using System.Data;
using DLV.Data.Entities;
using DLV.FrameWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditCommission : AdminPageBase
    {
        private CommissionInfo _commissionInfo = new CommissionInfo();
        private readonly CommissionController _commissionController = new CommissionController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["CommissionId"]))
                _commissionInfo = _commissionController.GetCommission(int.Parse(Request.QueryString["CommissionId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadUser();
            LoadSchedule();

            if (_commissionInfo.CommissionId == -1) return;
            RenderForm();
        }
        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }

        public void SaveData()
        {
            _commissionInfo.Price = decimal.Parse(txtPrice.Text);
            _commissionInfo.Quantity = int.Parse(txtQuantity.Text);
            _commissionInfo.Recipient = txtRecipient.Text;
            _commissionInfo.Status = int.Parse(ddlStatus.SelectedValue);
            _commissionInfo.Total = int.Parse(txtQuantity.Text)*decimal.Parse(txtPrice.Text);
            _commissionInfo.ScheduleId = int.Parse(ddlSchedule.SelectedValue);
            if (_commissionInfo.CommissionId == -1)
            {
                _commissionInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _commissionInfo.CreatedDate = DateTime.Now;
                _commissionController.InsertCommission(_commissionInfo);
            }
            else
            {
                _commissionInfo.UpdatedBy = UserId;
                _commissionInfo.UpdatedDate = DateTime.Now;
                _commissionController.UpdateCommission(_commissionInfo);
            }
            Response.Redirect("/Pages/ListCommission.aspx?xml=Commission");
        }

        public void RenderForm()
        {
            txtPrice.Text = _commissionInfo.Price.ToString();
            txtQuantity.Text = _commissionInfo.Quantity.ToString();
            txtRecipient.Text = _commissionInfo.Recipient;
            ddlStatus.SelectedValue = _commissionInfo.Status.ToString();
            ddlAdminUser.SelectedValue = _commissionInfo.CreatedBy.ToString();
            ddlSchedule.SelectedValue = _commissionInfo.ScheduleId.ToString();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListCommission.aspx?xml=Commission");
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "UserName";
            ddlAdminUser.DataValueField = "UserId";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadSchedule()
        {
            ddlSchedule.DataSource = new ScheduleController().ListSchedule().Where(p => p.Status == (int)Globals.DocumentStatus.Use).OrderBy(p => p.ScheduleName);
            ddlSchedule.DataTextField = "ScheduleName";
            ddlSchedule.DataValueField = "ScheduleId";
            ddlSchedule.DataBind();

            ddlSchedule.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }
    }
}